package com.qiaofang.collectmanage.facade.impl;

import java.util.List;
import javax.validation.Valid;

import com.qiaofang.collectmanage.stub.request.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.collectmanage.stub.constants.CrawlerSubTaskResponseCode;
import com.qiaofang.collectmanage.service.CrawlerSubTaskService;
import com.qiaofang.collectmanage.stub.bean.CrawlerSubTaskDTO;
import com.qiaofang.collectmanage.stub.facade.CrawlerSubTaskFacade;
import com.qiaofang.collectmanage.stub.response.GetSubTaskBySubTaskResponse;

import com.qiaofang.collectmanage.stub.response.ListSubTaskResponse;
import com.qiaofang.collectmanage.stub.response.RemoveSubTaskResponse;
import com.qiaofang.collectmanage.stub.response.SaveSubTaskResponse;
import com.qiaofang.collectmanage.stub.response.SearchSubTaskResponse;
import com.qiaofang.collectmanage.stub.response.UpdateSubTaskResponse;

import com.qiaofang.collectmanage.stub.response.GetSubTaskByUuidResponse;
import com.qiaofang.collectmanage.stub.response.ListSubTaskByUuidsResponse;
import com.qiaofang.collectmanage.stub.request.RemoveCrawlerSubTaskRequest;

/**
* Created by generator on 2018-8-1 14:39:02
*/
@RestController
public class CrawlerSubTaskFacadeImpl implements CrawlerSubTaskFacade {


    @Autowired
    private CrawlerSubTaskService subTaskService;


   @Override
    public GetSubTaskByUuidResponse getCrawlerSubTaskByUuid(@RequestBody @Valid GetCrawlerSubTaskByUuidRequest request){
       GetSubTaskByUuidResponse  response = new GetSubTaskByUuidResponse();
       CrawlerSubTaskDTO dto = subTaskService.getCrawlerSubTaskByUuid(request.getUuid());
       response.setSubTaskDTO(dto);
       response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
       response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
      return response;
    }
    @Override
    public GetSubTaskBySubTaskResponse getCrawlerSubTaskBySubTask(@RequestBody @Valid GetCrawlerSubTaskBySubTaskRequest request){
        GetSubTaskBySubTaskResponse  response = new GetSubTaskBySubTaskResponse();
        CrawlerSubTaskDTO dto = subTaskService.getCrawlerSubTaskBySubTask(request.getSubTaskDTO());
        response.setSubTaskDTO(dto);
        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }

    @Override
    public SaveSubTaskResponse saveCrawlerSubTask(@RequestBody @Valid SaveCrawlerSubTaskRequest request){
        SaveSubTaskResponse  response = new SaveSubTaskResponse();
        int count = subTaskService.saveCrawlerSubTask(request.getSubTaskDTO());
        if(count <= 0){
            response.setResponseCode(CrawlerSubTaskResponseCode.RC_SAVE_ERROR.getResponseCode());
            response.setResponseMessage(CrawlerSubTaskResponseCode.RC_SAVE_ERROR.getResponseMessage());
         }
        return response;
    }
    @Override
    public  UpdateSubTaskResponse updateCrawlerSubTask(@RequestBody @Valid UpdateCrawlerSubTaskRequest request){
        UpdateSubTaskResponse  response = new UpdateSubTaskResponse();
        int count = subTaskService.updateCrawlerSubTask(request.getSubTaskDTO());
        if(count <= 0){
            response.setResponseCode(CrawlerSubTaskResponseCode.RC_UPDATE_ERROR.getResponseCode());
            response.setResponseMessage(CrawlerSubTaskResponseCode.RC_UPDATE_ERROR.getResponseMessage());
        }
        return response;
    }

    @Override
    public  RemoveSubTaskResponse removeCrawlerSubTask (@RequestBody @Valid RemoveCrawlerSubTaskRequest request){
        RemoveSubTaskResponse  response = new RemoveSubTaskResponse();
        int count = subTaskService.removeCrawlerSubTaskByUuid(request.getUuid());
        if(count <= 0){
            response.setResponseCode(CrawlerSubTaskResponseCode.RC_REMOVE_ERROR.getResponseCode());
            response.setResponseMessage(CrawlerSubTaskResponseCode.RC_REMOVE_ERROR.getResponseMessage());
        }
        return response;
    }
    @Override
    public  SearchSubTaskResponse searchCrawlerSubTask (@RequestBody @Valid SearchCrawlerSubTaskRequest request){
        SearchSubTaskResponse response = new SearchSubTaskResponse();
        Long count = subTaskService.countCrawlerSubTaskByPermission(request.getSearchSubTaskDTO());
        if(count > 0){
          response.setList(subTaskService.searchCrawlerSubTaskByPermission(request.getSearchSubTaskDTO(),request.getPageDTO()));
        }
        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }
    @Override
    public  ListSubTaskResponse listCrawlerSubTask (@RequestBody @Valid ListCrawlerSubTaskRequest request){
         ListSubTaskResponse response = new ListSubTaskResponse();
         List<CrawlerSubTaskDTO> data = subTaskService.listCrawlerSubTask(request.getSubTaskDTO());
         response.setList(data);
         response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
         response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
         return response;
    }


   @Override
    public ListSubTaskByUuidsResponse listCrawlerSubTaskByUuids (@RequestBody @Valid ListCrawlerSubTaskByUuidsRequest request){
        ListSubTaskByUuidsResponse response = new ListSubTaskByUuidsResponse();
        List<CrawlerSubTaskDTO> data = subTaskService.listCrawlerSubTaskByUuids(request.getSubTaskUuidList());
        response.setList(data);
        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }
}
